package com.oocl.scan.producer;

import com.oocl.scan.io.FileReader;
import com.oocl.scan.jdt.parser.JavaParser;
import com.oocl.scan.model.JavaClassElement;
import com.oocl.scan.store.JavaClassStore;
import java.io.File;
import java.io.IOException;

public class ElementProducer implements Producer<File> {

  private JavaParser parser = new JavaParser();

  @Override
  public void produce(File file) {
    if (!file.exists()) {
      return;
    }
    if (FileReader.isNonTestJavaClass(file.getName())) {
      buildClass(file.getAbsolutePath());
    }
  }

  private void buildClass(String filePath) {
    try {
      String resource = FileReader.read(filePath);
      String moduleName = FileReader.getModuleName(filePath);
      String domainName = FileReader.getDomainName(filePath);
      JavaClassElement javaClassElement = parser.buildClass(resource, moduleName, domainName);
      if (javaClassElement != null) {
        javaClassElement.buildFieldMap();
        javaClassElement.buildImportMap();
        JavaClassStore.getInstance().add(javaClassElement);
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println("error in reading file:" + filePath);
    }
  }

}
